﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TencentCloud.Common.Profile;
using TencentCloud.Common;
using TencentCloud.Tiia.V20190529.Models;
using TencentCloud.Tiia.V20190529;

namespace ImageQueryImageApp
{
    internal static class TXHelper
    {
        public static string UploadImage(string filePath)
        {
            try
            { 
                Credential cred = new Credential
                {
                    SecretId = "AKIDnkW5ScZgfR25zhNsEPOFuNdnkW6VkngO",
                    SecretKey = "b8YmpgJk0FML9lBSYTBh1B9vXnzyc7f0"
                };
                // 实例化一个client选项，可选的，没有特殊需求可以跳过
                ClientProfile clientProfile = new ClientProfile();
                // 实例化一个http选项，可选的，没有特殊需求可以跳过
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("tiia.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                // 实例化要请求产品的client对象,clientProfile是可选的
                TiiaClient client = new TiiaClient(cred, "ap-shanghai", clientProfile);
                // 实例化一个请求对象,每个接口都会对应一个request对象
                CreateImageRequest req = new CreateImageRequest();
                req.GroupId = "tuku1";
                req.EntityId = Guid.NewGuid().ToString();
                req.PicName = req.EntityId;
                req.ImageBase64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(filePath));
                // 返回的resp是一个CreateImageResponse的实例，与请求对象对应
                CreateImageResponse resp = client.CreateImageSync(req);
                // 输出json格式的字符串回包
                return AbstractModel.ToJsonString(resp);
            }
            catch (Exception e)
            {
                return $"Error: {e.Message}";
            }
        }

        public static string QueryImage(string filePath)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = "AKIDnkW5ScZgfR25zhNsEPOFuNdnkW6VkngO",
                    SecretKey = "b8YmpgJk0FML9lBSYTBh1B9vXnzyc7f0"
                };
                // 实例化一个client选项，可选的，没有特殊需求可以跳过
                ClientProfile clientProfile = new ClientProfile();
                // 实例化一个http选项，可选的，没有特殊需求可以跳过
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("tiia.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                // 实例化要请求产品的client对象,clientProfile是可选的
                TiiaClient client = new TiiaClient(cred, "ap-shanghai", clientProfile);
                // 实例化一个请求对象,每个接口都会对应一个request对象

                SearchImageRequest req = new SearchImageRequest();
                req.GroupId = "tuku1";
                req.ImageBase64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(filePath));
                req.MatchThreshold = 50;

                // 返回的resp是一个CreateImageResponse的实例，与请求对象对应
                SearchImageResponse resp = client.SearchImageSync(req);
                // 输出json格式的字符串回包
                return AbstractModel.ToJsonString(resp);
            }
            catch (Exception e)
            {
                return $"Error: {e.Message}";
            }
        }
    }
}
